<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleExport()">
          <Icon icon="i-ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <a-button type="default" @click="handleImport()">
          <Icon icon="i-ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button>
        <a-button type="default"  @click="handlePrint({})" >
          <Icon icon="i-simple-line-icons:printer" /> {{ t('打印') }}
        </a-button>
        <a-button type="primary" @click="handleForm({})" v-auth="'bas:cus:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ code: record.code })">
          {{ record.code }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
    <PrintModal @register="registerPrintModal" />
  </div>
</template>
<script lang="ts" setup name="ViewsBasCusBasCusList">
  import { unref, watch } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { basCusDelete, basCusListData } from '/@/api/bas/cus/basCus';
  import { basCusDisable, basCusEnable } from '/@/api/bas/cus/basCus';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';
  import { basCusClsTreeData } from '/@/api/bas/cus/basCusCls';
  import PrintModal from '/@/components/print/printModal.vue';

  const props = defineProps({
    treeCode: String,
    treeName: String,
  });

  const { t } = useI18n('bas.cus.basCus');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('客户档案管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('客户编码'),
        field: 'code',
        component: 'Input',
      },
      {
        label: t('客户名称'),
        field: 'name',
        component: 'Input',
      },
      {
        label: t('客户分类'),
        field: 'cusCls.code',
        fieldLabel: 'cusCls.name',
        component: 'TreeSelect',
        componentProps: {
          api: basCusClsTreeData,
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('客户简称'),
        field: 'cusAbbName',
        component: 'Input',
      },
      {
        label: t('联系人'),
        field: 'cusPerson',
        component: 'Input',
      },
      {
        label: t('电话'),
        field: 'cusHand',
        component: 'Input',
      },
      {
        label: t('联系地址'),
        field: 'cusAddress',
        component: 'Input',
      },
      {
        label: t('邮箱'),
        field: 'cusEmail',
        component: 'Input',
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'sys_search_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('备注'),
        field: 'remarks',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('客户编码'),
      dataIndex: 'code',
      key: 'a.code',
      sorter: true,
      width: 150,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('客户名称'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('客户简称'),
      dataIndex: 'cusAbbName',
      key: 'a.cus_abb_name',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('客户分类'),
      dataIndex: 'cusCls.name',
      key: 'a.cls_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('联系人'),
      dataIndex: 'cusPerson',
      key: 'a.cus_person',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('电话'),
      dataIndex: 'cusHand',
      key: 'a.cus_hand',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('联系地址'),
      dataIndex: 'cusAddress',
      key: 'a.cus_address',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('邮箱'),
      dataIndex: 'cusEmail',
      key: 'a.cus_email',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'sys_search_status',
    },
    {
      title: t('更新时间'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑客户档案'),
        onClick: handleForm.bind(this, { code: record.code }),
        auth: 'bas:cus:edit',
      },
      {
        icon: 'i-ant-design:stop-outlined',
        color: 'error',
        title: t('停用客户档案'),
        popConfirm: {
          title: t('是否确认停用客户档案'),
          confirm: handleDisable.bind(this, record),
        },
        auth: 'bas:cus:edit',
        ifShow: () => record.status === '0',
      },
      {
        icon: 'i-ant-design:check-circle-outlined',
        color: 'success',
        title: t('启用客户档案'),
        popConfirm: {
          title: t('是否确认启用客户档案'),
          confirm: handleEnable.bind(this, record),
        },
        auth: 'bas:cus:edit',
        ifShow: () => record.status === '2',
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除客户档案'),
        popConfirm: {
          title: t('是否确认删除客户档案'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'bas:cus:edit',
      },
    ],
  };


  const [registerPrintModal, { openModal: openPrintModal }] = useModal();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm,getSelectRows }] = useTable({
    api: basCusListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  watch(
    () => props.treeCode,
    async () => {
      await getForm().setFieldsValue({
        'cusCls.code': props.treeCode,
        'cusCls.name': props.treeName,
      });
      reload();
    },
  );

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/bas/cus/basCus/exportData',
      params: getForm().getFieldsValue(),
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  async function handleDisable(record: Recordable) {
    const params = { code: record.code };
    const res = await basCusDisable(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleEnable(record: Recordable) {
    const params = { code: record.code };
    const res = await basCusEnable(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleDelete(record: Recordable) {
    const params = { code: record.code };
    const res = await basCusDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  const handlePrint = async () => {
    let rows = getSelectRows().map((item)=>{
      return item.code
    });
    if (rows.length <= 0) {
      showMessage('请至少选择一条要打印的数据!');
    } else {
      let ids = rows.join(',');
      let obj = {
        title: '客户打印',
        ids: ids,
        height: '0.8',
        width: '0.6',
        fileName: 'printCus',
      };
      openPrintModal(true, obj);
    }
  };
</script>
